﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FizzBuzz
{
    internal class Product
    {
        public string Name
        {
            get;
            private set;
        }

        public decimal Price
        {
            get;
            private set;
        }

        public Product(string name, decimal price)
        {
            Name = name;
            Price = price;
        }

        private Product()
        {
        }

        public static List<Product> GetSampleProducts()
        {
            return new List<Product>
            {
                new Product
                {
                    Name = "Company",
                    Price = 9.99m
                }
            ,
                new Product
                {
                    Name = "Assassins",
                    Price = 14.99m
                }
            ,
                new Product
                {
                    Name = "Frogs",
                    Price = 13.99m
                }
            ,
                new Product
                {
                    Name = "Sweeney Todd",
                    Price = 10.99m
                }
            }
            ;
        }

        public override string ToString()
        {
            return string.Format("{0}: {1}", Name, Price);
        }
    }

    internal class ProductNameComparer : IComparer<Product>
    {
        public int Compare(Product first, Product second)
        {
            return first.Name.CompareTo(second.Name);
        }
    }

    internal class Looping
    {
        public void Run()
        {
            List<Product> products = Product.GetSampleProducts();
            Predicate<Product> test = delegate(Product p)
                                          { return p.Price > 10m; };

            List<Product> matches = products.FindAll(test);

            Action<Product> print = delegate(Product p)
                                        { Console.WriteLine(p); };

            matches.ForEach(print);

            foreach(Product p in products.Where(p => p.Price > 10m))
            {
                Console.WriteLine(p);
            }

            var filtered = from Product p2 in products
                           where p2.Price > 10
                           select p2;

            foreach(Product product in filtered)
            {
                Console.WriteLine(product);
            }



        }
    }

    internal class SortDemo
    {
        public void Run()
        {
            List<Product> products = Product.GetSampleProducts();
            // C# 2.0
//            products.Sort(new ProductNameComparer());
//            foreach(Product p in products)
//            {
//                Console.WriteLine(p.Name);
//            }

            // C# 2.0 - anonymous delegate
//            products.Sort(delegate(Product first, Product second)
//                              {
//                                  return first.Name.CompareTo(second.Name);
//                              });

            // C# 3.0 - lambda expressions
            products.Sort((first, second) => first.Name.CompareTo(second.Name));

            foreach (Product p in products)
            {
                Console.WriteLine(p.Name);
            }

            // C# 3.0 - extension method

        }
    }
}